package graduate.wangyaqi.Taoti;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Optional;

@RestController
@CrossOrigin(origins = "*", maxAge = 3600)
@RequestMapping(path="/TaoTi")
public class TaoTiController {
    @Autowired
    private TaotiService taotiService;

    @GetMapping(path="/findById")
    public Optional<Taoti> findById(Long id){
        return taotiService.findById(id);
    }

    @GetMapping(path="/all")
    public @ResponseBody
    Iterable<Taoti> getAll() {
        return taotiService.getAll();
    }

    @GetMapping(path="/delete")
    public @ResponseBody
    void Del(Long id) {
        taotiService.delete(id);
    }

    @PostMapping(path="/insert")
    public boolean Insert(@RequestBody Taoti taoti){
        if (taoti.getId() != 0) {
            taoti = taotiService.findById(taoti.getId()).get();
        }
        taotiService.save(taoti);
        return true;
    }

    @PostMapping(path="/save")
    public boolean save(@RequestBody Taoti taoti){
        taoti.setJointime(new Date());
        taotiService.save(taoti);
        return true;
    }
}
